/**
 * Core Application
 */
Ext.BLANK_IMAGE_URL = 'javascripts/ext-2.0-beta1/resources/images/gray/s.gif';

Ext.app.App = function(cfg){
    Ext.apply(this, cfg);
    this.addEvents({
        'ready' : true,
        'beforeunload' : true
    });

    Ext.onReady(this.initApp, this);
};

Ext.extend(Ext.app.App, Ext.util.Observable, {
    isReady: false,
	modules: {},
	
    initApp : function(){
        this.init();
		
		this.viewport = new Ext.app.Viewport();
		var ms = this.addModules();
        if(ms){
            this.initModules(ms);
        }
        Ext.EventManager.on(window, 'beforeunload', this.onUnload, this);
		this.fireEvent('ready', this);
        this.isReady = true;
    },
	
	addModules : Ext.emptyFn,
	init : Ext.emptyFn,

	initModules : function(ms){
		for(var i = 0, len = ms.length; i < len; i++){
            var m = ms[i];
			m.app = this;
			m.run();
			this.modules[m.id] = m;
        }
    },

    onReady : function(fn, scope){
        if(!this.isReady){
            this.on('ready', fn, scope);
        }else{
            fn.call(scope, this);
        }
    },

    onUnload : function(e){
        if(this.fireEvent('beforeunload', this) === false){
            e.stopEvent();
        }
    }
});